package com.xmut.eduservice.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.xmut.commonutils.R;
import com.xmut.eduservice.service.VodService;
import com.xmut.eduservice.util.AliyunVodSDKUtils;
import com.xmut.eduservice.util.ConstantPropertiesUtil;
import com.xmut.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;

    // 上传视频到阿里云
    @PostMapping("uploadVideo")
    public R uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {
        String videoId = vodService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("videoId", videoId);
    }

    // 获取sh视频播放地址
    @GetMapping("getVideoUrl/{video_source_id}")
    public R getVideoUrl(@PathVariable String video_source_id) {
        // 初始化对象
        try {

            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
//            GetPlayInfoResponse response = new GetPlayInfoResponse();

            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(video_source_id);
            GetPlayInfoResponse response = client.getAcsResponse(request);
//
//            response = getPlayInfo(client);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            List<String> videoUrlList = new ArrayList<>();
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
                videoUrlList.add(playInfo.getPlayURL());
            }
            //Base信息
            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");

            System.out.print("RequestId = " + response.getRequestId() + "\n");
            return R.ok().data("videoUrl", videoUrlList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "获取视频播放地址失败");
        }
    }

    // 根据id删除一个视频
    @DeleteMapping("removeVideo/{videoId}")
    public R removeVideo(@PathVariable String videoId) {
        try {
            // 初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            // 创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 向request设置视频id
            request.setVideoIds(videoId);
            // 调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }
    }

    // 删除多个视频
    // 参数多个id
    @DeleteMapping("delete-batch")
    public R removeVideoList(
            @ApiParam(name = "videoIdList", value = "云端视频id", required = true)
            @RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeVideoList(videoIdList);
        return R.ok().message("视频删除成功");
    }
}
